<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="topbar::static">

<body>

<nav th:replace="topbar::topbar"></nav>

<div class="container-fluid">
    <div class="row">
        <div th:replace="topbar::sidebar(activeUri='vdu.html')"></div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h1 class="page-header">5GC MANO</h1>
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg">
                新增VDU
            </button>
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>vduId</th>
                        <th>name</th>
                        <th>description</th>
                        <th>intCpd</th>
                        <th>osContainerDesc</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="vdu:${vdus}">
                        <td th:text="${vdu.vduId}"></td>
                        <td th:text="${vdu.name}"></td>
                        <td th:text="${vdu.description}"></td>
                        <td th:text="${vdu.intCpd}"></td>
                        <td th:text="${vdu.osContainerDesc}"></td>
                        <td>
                            <button type="button" class="btn btn-danger" aria-label="Left Align"
                                    th:onclick="|deleteVdu(${vdu.vduId})|">
                                <span class="glyphicon glyphicon-remove"></span>
                            </button>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!--模态框，用于新增vdu-->
<div id="modal_block" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
     aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">新增VDU</h4>
            </div>
            <div class="modal-body">
                <table class="table table-striped">
                    <div class="table-responsive">
                        <thead>
                        <tr>
                            <th>vduId</th>
                            <th>name</th>
                            <th>description</th>
                            <th>intCpd</th>
                            <th>osContainerDesc</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td><input type="text" class="form-control" id="vduId"></td>
                            <td><input type="text" class="form-control" id="name"></td>
                            <td><input type="text" class="form-control" id="description"></td>
                            <td>
                                <select id="intCpd" class="selectpicker" multiple>
                                    <option class="form-control" th:each="vducpd:${vducpds}"
                                            th:value="${vducpd.cpdId}"
                                            th:text="|protocol:${vducpd.layerProtocol},bitrateReq:${vducpd.bitrateRequirement},port:${vducpd.port}|">
                                    </option>
                                </select>
                            </td>
                            <td>
                                <select id="osContainerDesc" class="selectpicker" multiple>
                                    <option class="form-control" th:each="oscontainerdesc:${oscontainerdescs}"
                                            th:value="${oscontainerdesc.osContainerDescId}"
                                            th:text="|cpu:${oscontainerdesc.requestedCpuResources},memory:${oscontainerdesc.requestedMemoryResources},image:${oscontainerdesc.swImageDesc}|">
                                    </option>
                                </select>
                            </td>
                        </tr>
                        </tbody>
                    </div>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="createVdu()">保存</button>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var baseUrl = "http://"+hostIp+":8004/vdu/";

    function createVdu() {
        var url = baseUrl + "create";
        $.ajax({
            url: url,
            type: "POST",
            data: {
                "vduId": $("#vduId").val(),
                "name": $("#name").val(),
                "description": $("#description").val(),
                "intCpd": $("#intCpd").val().join(","),
                "osContainerDesc": $("#osContainerDesc").val().join(",")
            },
            complete: function (vnfdId) {
                alert("vnfd创建成功");
                reload();
            }
        });
    }

    function deleteVdu(vduId) {
        //异步删除nsd
        var url = baseUrl + "delete";
        $.ajax({
            url: url,
            type: "POST",
            data: {
                "vduId": vduId
            },
            complete: function (vduId) {
                alert("nsd删除成功");
                reload();
            }
        });
    }

    function reload() {
        $("#modal_block").modal('hide');
        window.location.reload();
    }
</script>
</html>
